package com.traffic.fuwu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.traffic.fuwu.dao.AccountMapper;
import com.traffic.fuwu.mybatis.pojo.Account;
import com.traffic.fuwu.mybatis.pojo.AccountExample;
import com.traffic.fuwu.service.AccountService;
@Service
public class AccountServiceImpl implements AccountService{
	@Autowired
	private AccountMapper accountMapper;

	@Override
	public Account login(String accountName, String accountPassword) {
		AccountExample example = new AccountExample();
		example.or().andAccountNameEqualTo(accountName).andAccountPasswordEqualTo(accountPassword);
		
		List<Account> account1 = accountMapper.selectByExample(example);
		if(account1 != null && account1.size() != 0	){
			Account account = account1.get(0);
			if(account != null && account.getAccountName().equals(accountName)){
				return account;
			}
		}
		return null;
	}

	@Override
	public void updateAccount(Account account) {
		this.accountMapper.updateByPrimaryKey(account);
		
	}

	@Override
	public void addAccount(Account account) {
		accountMapper.insert(account);
	}

	@Override
	public Account getById(int accountId) {
		AccountExample example = new AccountExample();
		example.or().andAccountIdEqualTo(accountId);
		try {
			List<Account> list = accountMapper.selectByExample(example);
			if(list != null && list.size() != 0){}
			Account account = list.get(0);
			return account;
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Account getByAccountName(String accountName) {
		AccountExample example = new AccountExample();
		example.or().andAccountNameEqualTo(accountName);
		try {
			List<Account> list = accountMapper.selectByExample(example);
			if(list != null && list.size() != 0){
				Account account = list.get(0);
				return account;
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

}
